<?php
/** Zend_Controller_Action */ 
require_once APPLICATION_PATH . '/../library/Soooner/Controller/Action.php';
class ProjectController extends Soooner_Controller_Action
{
	private $_id;
	private $_modelProject;
	private $_modelWorkflow;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelProject = new Model_Tables_Project();
    	$this->_modelWorkflow = new Model_Tables_Workflow();
    }
    public function advsearchAction(){
    	include $this->_dzTemplate->getFile( 'project.advsearch.html' );
    }
    /**
    * 获取项目列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	/*$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $ItemCount =  $this->_modelProject->getCount();
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;*/
		include $this->_dzTemplate->getFile( 'project.manage.html' );
    }
    /**
    * 获取项目列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
    	$deptmodel = new Model_Tables_Department();
    	$deptsList = $deptmodel->getDeptList();
    	$adminmodel = new Model_Tables_Admin();
    	$adminList = $adminmodel->getAdminList();
    	//print_r($adminList);
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		//$s = $this->_getParam('s'); 
   		$json = $_POST['s'];
   		$r=$this->j2Array($json);
   		$r["members"]=$this->_userinfo["userid"];
   		//print_r($r);
        $ItemCount =  $this->_modelProject->getCount($r);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos0 = $this->_modelProject->getList( $page, $numPerPage, $r );
        foreach($infos0 as $item){
        	$item["department"] = $deptsList[$item["department"]];
        	$item["dutyperson"] = $adminList[$item["dutyperson"]];
        	$infos[] = $item;
        }
        
        include $this->_dzTemplate->getFile( 'ajax/project.manage.ajax.html' );
    }
    /**
    * 添加项目
    * @return unknown_type
    */
    public function addAction()
    {
    	$request = $this->getRequest();    
    	$deptmodel = new Model_Tables_Department();
    	$deptsList = $deptmodel->getDeptList();
    	$workflowList = $this->_modelWorkflow->getWorkflowList();
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelProject->add($params);                
            $this->_redirect("/project/index/");
        } 
        include $this->_dzTemplate->getFile( 'project.add.html' );
    }
    /**
    * 修改项目
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$id = $this->_getParam('id');
    	$info = $this->_modelProject->getInfo($id); 
        $request = $this->getRequest();      
        $deptmodel = new Model_Tables_Department();
        $deptsList = $deptmodel->getDeptList();
        $workflowList = $this->_modelWorkflow->getWorkflowList();
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelProject->modify($request,$id);
                $this->_redirect("/project/");
            }
        } 
        include $this->_dzTemplate->getFile( 'project.edit.html' );
    }
    /**
    * 无刷新修改项目
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $id = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelProject->modifyAjax($id,$field,$value);
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelProject->modifyAjaxRevers($field,$pri);
        exit;
    }
    /**
    * 删除项目
    */
    public function deleteAction()
    {
    	$id = $this->_getParam('id');
    	$num = $this->_modelProject->getCompoundNum($id);
    	if($num == 0)
    		$this->_modelProject->del($id);
    	echo $num;
    }
    public function j2array($j){
    	$json=str_replace("\\", "", $j);
    	$a = json_decode($json);
    	$r = array();
    	foreach($a as $item){
    		$r[$item->name]=$item->value;
    	}
    	return $r;
    }
    public function testAction(){
    	print_r($this->_userinfo);
    }
}